﻿#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.dll"
#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.Runtime.dll"

#load "Client.fs"

open Client
open Microsoft.SharePoint.Client
open Microsoft.SharePoint.Client.Sharing
open Microsoft.SharePoint.Client.Utilities
open Microsoft.SharePoint.Client.WebParts
open Microsoft.SharePoint.Client.Application
open System.Security
open System

// =======================================================
// Adding site, web and list event receivers
// =======================================================

let site = Client.Context.Site
Client.Load site

let web = Client.Context.Web
Client.Load web

let list = web.Lists.GetByTitle("Documents")

Client.Load list

list.OnQuickLaunch <- true
Client.ExecuteQuery()

Client.Load list.EventReceivers
Client.Load site.EventReceivers
Client.Load web.EventReceivers

let ereci = new EventReceiverDefinitionCreationInformation()
ereci.EventType <- EventReceiverType.ListAdded
ereci.ReceiverName <- "DocEventReceiver"
ereci.ReceiverUrl <- "https://geek.cloudapp.com/DocEventReceiver.svc"
ereci.SequenceNumber <- 10

web.EventReceivers.Add(ereci)
Client.ExecuteQuery()

let mutable i = 0
for receiver in list.EventReceivers do
    Client.Load receiver
    let msg = i.ToString() + " " + receiver.ReceiverName + " " + receiver.EventType.ToString()+ " " + receiver.ReceiverUrl+ " " + receiver.ReceiverAssembly+ " " + receiver.ReceiverClass
    i <- i+1
    printfn "%s\n" msg

let receiver = web.EventReceivers.[1]
Client.Load receiver
receiver.DeleteObject()
Client.ExecuteQuery()